package net.easyjava.test.aws;

import com.amazon.sqs.javamessaging.AmazonSQSMessagingClientWrapper;
import com.amazon.sqs.javamessaging.SQSConnection;
import com.amazon.sqs.javamessaging.SQSConnectionFactory;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.sqs.model.CreateQueueResult;

import javax.jms.*;

/**
 * Created by wxnacy on 16/11/23.
 */
public class SQSJMSDemo {
    private static SQSConnectionFactory connectionFactory;
    private static SQSConnection connection;
    public static final String QUEUE_EASYJAVA_TEST = "easyjava-test";

    public static void main(String[] args) {
        init();

        try {
            //创建队列
//            createQueue();
            //同步发送消息
            sendMessage();

        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

    /**
     * 初始化
     */
    private static void init() {
        try {
            connectionFactory =
                    SQSConnectionFactory.builder()
                            .withRegion(Region.getRegion(Regions.US_EAST_1))
                            .withAWSCredentialsProvider(new AWSSQSCredentialsProvider())
                            .build();
            connection = connectionFactory.createConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建队列
     * @throws JMSException
     */
    private static void createQueue() throws JMSException {
        AmazonSQSMessagingClientWrapper client = connection.getWrappedAmazonSQSClient();
        CreateQueueResult result = null;

        if (!client.queueExists(QUEUE_EASYJAVA_TEST)) {
            result = client.createQueue(QUEUE_EASYJAVA_TEST);
            System.out.println("result.toString() = " + result.toString());
        }

    }

    /**
     * 同步发送消息
     */
    private static void sendMessage() throws JMSException {
        //当连接和基础 Amazon SQS 队列准备就绪时，将创建一个具有 AUTO_ACKNOWLEDGE 模式的非事务性 JMS 会话。
        Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
        //为了向队列发送文本消息，将创建一个 JMS 队列标识和消息创建者。
        Queue queue = session.createQueue(QUEUE_EASYJAVA_TEST);
        MessageProducer mp = session.createProducer(queue);
        //创建文本消息并将它发送到队列。
        TextMessage tm = session.createTextMessage("hello easyjava");
        mp.send(tm);
        System.out.println("tm.getJMSMessageID() = " + tm.getJMSMessageID());


        receiveMessage(session,queue);
    }


    /**
     * 同步接收消息
     */
    private static void receiveMessage(Session session,Queue queue) throws JMSException {
        //要接收消息，可在同一队列上创建一个使用者，然后调用 start 方法。您可以随时对连接调用 start 方法。不过，使用者将不会开始接收消息，直至调用此方法。
        MessageConsumer consumer = session.createConsumer(queue);
        connection.start();
        //在超时设为 1 秒钟的情况下对使用者调用 receive 方法，然后输出收到的消息内容。
        Message receivedMessage = consumer.receive(1000);
        if (receivedMessage != null) {
            System.out.println("Received: " + ((TextMessage) receivedMessage).getText());
        }
        //关闭连接和会话。
        connection.close();

    }

    private static void asyncReceiveMessage(){

    }











    /**
     * 重写生产类
     */
    public static class AWSSQSCredentialsProvider implements AWSCredentialsProvider{

        @Override
        public AWSCredentials getCredentials() {
            return new BasicAWSCredentials(AWSConstants.KEY_ID, AWSConstants.KEY_SECRET);
        }

        @Override
        public void refresh() {

        }
    }


}
